#include "utils/init.h"
#include "utils/glutil.h"
#include "utils/logger.h"
#include <psp2/kernel/threadmgr.h>

#include <falso_jni/FalsoJNI.h>
#include <so_util/so_util.h>
#include "reimpl/controls.h"
#include <stdio.h>
int _newlib_heap_size_user = 256 * 1024 * 1024;

#ifdef USE_SCELIBC_IO
int sceLibcHeapSize = 4 * 1024 * 1024;
#endif
#define SCREEN_W 960
#define SCREEN_H 544
so_module so_mod;
#define CONFIG_FILE_PATH DATA_PATH "config.txt"

jobjectArray JobjectArray;
int StartOffset;

void loadConfig() {
    char buffer[30];
    int value;
    StartOffset=0;
    FILE *config = fopen(CONFIG_FILE_PATH, "r");

    if (config) {
        while (EOF != fscanf(config, "%[^ ] %d\n", buffer, &value)) {
            if (strcmp("StartOffset", buffer) == 0) 
                 StartOffset = value;
          
        }
        fclose(config);
    }
}

int main_thread(SceSize args, void *argp)
{
	int (* NativeFrame)(void) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnNativeFrame");
     while (1) {
          controls_poll();
          NativeFrame();
          gl_swap();
     }
    debugPrintf("RenderUpdate over \n");
	return 0;
}

void Login()
{
    int (* OnOthersLoginResult)(void *,int,char *,int) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnOthersLoginResult");
    OnOthersLoginResult(&jni,0,"2365645585",1);
}

int main() {
    soloader_init_all();
    loadConfig();
    // int (* JNI_OnLoad)(void *jvm) = (void *)so_symbol(&so_mod, "JNI_OnLoad");
    // JNI_OnLoad();
    gl_init();
    int (* OnNativeInitializeJNI)(void *) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_ItarosActivity_OnNativeInitializeJNI");
    int (* NativePath)(void *,int,char *) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnNativePath");
    int (* NativeInitialize)(void *) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnNativeInitialize");
    int (* NativeSize)(int,int,int,int) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnNativeSize");
	int (* VFSInitialize)(void *,int,int,int,int,int,int) = (void *)so_symbol(&so_mod, "Java_com_Nexon_DunfightENGF1_NativeClass_OnVFSInitialize");

    OnNativeInitializeJNI(&jni);
    debugPrintf("OnNativeInitializeJNI over \n");

    NativePath(&jni,0,"ux0:data/DunfightENGF1");
    debugPrintf("NativePath over \n");
    
    VFSInitialize(&jni,0,1,0,0,0,52108710);
    debugPrintf("VFSInitialize over \n");

    NativeInitialize(&jni);
    debugPrintf("NativeInitialize over \n");
    
    //eglSwapInterval(0, 2);

    NativeSize(0,0,SCREEN_W,SCREEN_H);
    debugPrintf("NativeSize over \n");

    // ... do some initialization

    SceUID thid = sceKernelCreateThread("main_thread", (SceKernelThreadEntry)main_thread, 0x40,2* 128 * 1024, 0, 0, NULL);
	sceKernelStartThread(thid, 0, NULL);
	return sceKernelExitDeleteThread(0);
}
